package org.unicat.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import org.unicat.serviceedu.entity.EduSubject;
import org.unicat.serviceedu.entity.excel.ExcelSubjectData;
import org.unicat.serviceedu.entity.subject.OneSubject;
import org.unicat.serviceedu.entity.subject.TwoSubject;
import org.unicat.serviceedu.listener.ExcelSubjectListener;
import org.unicat.serviceedu.mapper.EduSubjectMapper;
import org.unicat.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author unicat
 * @since 2022-02-20
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) throws IOException {
        InputStream in = file.getInputStream();
        EasyExcel.read(in, ExcelSubjectData.class, new ExcelSubjectListener(subjectService))
                .sheet()
                .doRead();
    }

    @Override
    public void loadSubject() {
        String fileName = "W:\\Computer Science\\SpringBoot2\\Guli\\service\\service_edu\\src\\main\\resources\\static\\excel\\subject.xlsx";

        EasyExcel.write(fileName, EduSubject.class)
                .sheet()
                .doWrite(this.list());
    }

    @Override
    public List<OneSubject> getAllSubject() {
        List<EduSubject> oneLists = this.list(new QueryWrapper<EduSubject>().eq("parent_id", 0));
        List<EduSubject> twoLists = this.list(new QueryWrapper<EduSubject>().ne("parent_id", 0));

        List<OneSubject> subjectList = new ArrayList<>();

        // 一级列表遍历
        for (EduSubject oneList : oneLists) {
            // 如果已经加入了则下一条一级
            int j = 0;
            for (; j < subjectList.size(); j++) {
                if (subjectList.get(j).getTitle().equals(oneList.getTitle())) {
                    break;
                }
            }
            // 已经在列表里面了
            if (j != subjectList.size()) {
                break;
            }
            // 否则加入
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oneList, oneSubject);
            subjectList.add(oneSubject);
        }

        // 二级列表遍历
        for(EduSubject twoList : twoLists){
            int j = 0;
            for(; j < subjectList.size(); j++){
                // 遍历到了twolist的父节点
                if(subjectList.get(j).getId().equals(twoList.getParentId())){
                    break;
                }
            }
            // 遍历到了就加入到父列表的孩子当中
            if(j != subjectList.size()){
                TwoSubject twoSubject = new TwoSubject();
                BeanUtils.copyProperties(twoList, twoSubject);
                // 父节点添加孩子
                subjectList.get(j).getChildren().add(twoSubject);
            }
        }

        return subjectList;
    }

    /**
     * 判断一级分类不能重复
     *
     * @param title 分类名称
     * @return 存在则返回
     */
    @Override
    public EduSubject existOneSubject(String title) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title)
                .eq("parent_id", "0");
        return this.getOne(wrapper);
    }

    /**
     * 判断二级级分类不能重复
     *
     * @param title 二级名称
     * @param pid   上面的一级名称
     * @return
     */
    @Override
    public EduSubject existTwoSubject(String title, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title)
                .eq("parent_id", pid);
        return this.getOne(wrapper);
    }


}
